﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.IO.Compression;

namespace ZgOffice.Services
{
    public class DataTransfers
    {
        /// <summary>
        /// 无参数构造函数
        /// </summary>
        public DataTransfers()
        {

            InitBuffers(InitTestDataSet());
        }

        private byte[] buffer_all = null;
        private byte[] buffer_currect = null;
        private int get_buffer_length = 1000;
        private long remain_length;
        private MemoryStream stream;
        /// <summary>
        /// 生成一个测试的数据集
        /// </summary>
        /// <returns></returns>
        private DataSet InitTestDataSet()
        {
            DataSet ds = new DataSet("test");
            DataTable table = new DataTable("test");
            DataColumn column = new DataColumn("test");
            column.DataType = Type.GetType("System.String");
            table.Columns.Add(column);
            DataRow row;
            for (int i = 0; i < 100000; i++)
            {
                row = table.NewRow();
                row["test"] = "测试数据 !";
                table.Rows.Add(row);
            }

            ds.Tables.Add(table);

            return ds;

        }
        /// <summary>
        /// 初始化压缩字节流
        /// </summary>
        /// <param name="ds"></param>
        private void InitBuffers(DataSet ds)
        {

            IFormatter formatter = new BinaryFormatter();
            MemoryStream stream_ = new MemoryStream();
            formatter.Serialize(stream_, ds);
            buffer_all = stream_.ToArray();
            stream_.Close();
            byte[] bytes_c = Compression(buffer_all, CompressionMode.Compress);
            stream = new MemoryStream(bytes_c);
            stream.Position = 0;
            remain_length = stream.Length;


        }
        /// <summary>
        /// 提供内部使用压缩字流的方法
        /// </summary>
        /// <param name="data"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        private byte[] Compression(byte[] data, CompressionMode mode)
        {
            DeflateStream zip = null;
            try
            {
                if (mode == CompressionMode.Compress)
                {
                    MemoryStream ms = new MemoryStream();
                    zip = new DeflateStream(ms, mode, true);
                    zip.Write(data, 0, data.Length);
                    zip.Close();
                    return ms.ToArray();
                }
                else
                {
                    MemoryStream ms = new MemoryStream();
                    ms.Write(data, 0, data.Length);
                    ms.Flush();
                    ms.Position = 0;
                    zip = new DeflateStream(ms, mode, true);
                    MemoryStream os = new MemoryStream();
                    int SIZE = 1024;
                    byte[] buf = new byte[SIZE];
                    int l = 0;
                    do
                    {
                        l = zip.Read(buf, 0, SIZE);
                        if (l == 0) l = zip.Read(buf, 0, SIZE);
                        os.Write(buf, 0, l);
                    } while (l != 0);
                    zip.Close();
                    return os.ToArray();
                }
            }
            catch
            {
                if (zip != null) zip.Close();
                return null;
            }
            finally
            {
                if (zip != null) zip.Close();
            }
        }

        #region IDataTransfers 成员
        /// <summary>
        /// 获取所有字节流
        /// </summary>
        /// <returns></returns>
        public byte[] GetAllBuffer()
        {
            if (buffer_all != null)
                return buffer_all;
            else return null;
        }
        /// <summary>
        /// 设置压缩后字节流分块，每一块的大小
        /// </summary>
        /// <param name="length"></param>
        public void SetBufferLength(int length)
        {
            this.get_buffer_length = length;
        }
        /// <summary>
        /// 读取压缩后字节流一块，并提升字节流的位置
        /// </summary>
        /// <returns></returns>
        public bool ReadNextBuffer()
        {
            bool bo;
            if (remain_length > 0)
            {
                if (remain_length > get_buffer_length)
                {
                    buffer_currect = new byte[get_buffer_length];

                    stream.Read(buffer_currect, 0, get_buffer_length);
                    remain_length -= get_buffer_length;
                }
                else
                {
                    buffer_currect = new byte[remain_length];
                    stream.Read(buffer_currect, 0, (int)remain_length);
                    remain_length = 0;
                }

                bo = true;
            }
            else
                bo = false;
            return bo;

        }
        /// <summary>
        /// 获取当前块的字节流
        /// </summary>
        /// <returns></returns>
        public byte[] GetCurrentBuffer()
        {
            if (buffer_currect != null)
                return buffer_currect;
            else
                return null;

        }
        #endregion
    }
}
